package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.igas.bomp.facade.BompTransMonitorService;
import cc.rengu.igas.bomp.facade.request.KeySynchroniseRequest;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.igas.service.PTxnCfgService;
import cc.rengu.redp.bizimpl.upmp.controller.BatchController;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.RedpResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class KeySynchroniseServiceImpl implements KeySynchroniseService {

    @Autowired
    private IUpmpSysParamService iUpmpSysParamService;
    @Autowired
    private RealTaskService realTaskService;
    @Autowired
    private PTxnCfgService pTxnCfgService;
    @Autowired
    private IUpmpBMchntBaseInfoService iUpmpBMchntBaseInfoService;

    @DubboReference(version = "${dubbo.protocol.version}", check = false)
    private BompTransMonitorService bompTransMonitorService;

    private static final Logger logger = LoggerFactory.getLogger(BatchController.class);

    @Override
    public RedpResponse keySynchronise(HttpServletRequest request) {
        RedpResponse redpResponse = new RedpResponse();
        String dstChannelId = request.getParameter("dstChannelId");
        UpmpUserInfoVO upmpUserInfoVO = CommonUtils.getCurrentUser(iUpmpSysParamService);
        logger.info("法人行["+upmpUserInfoVO.getInstId()+"]用户["+upmpUserInfoVO.getId()+"]请求执行["+dstChannelId+"]"+ BompTxnNumEnum.keySynchronise.getDesc());
        if(!upmpUserInfoVO.isAdmin()){
            return redpResponse.message("法人行["+upmpUserInfoVO.getInstId()+"]用户["+upmpUserInfoVO.getId()+"]无权限操作!");
        }
        KeySynchroniseRequest req = new KeySynchroniseRequest();
        req.setDstChannelId(dstChannelId);
        req.setUserId(upmpUserInfoVO.getId());
        return realTaskService.keySynchronise(upmpUserInfoVO.getInstId(), req,bompTransMonitorService, iUpmpBMchntBaseInfoService.genMerOrder(upmpUserInfoVO.getInstId()));

    }
}
